//
//  UIBarButtonItem+Item.m
//  BuDeJie15
//
//  Created by xiaomage on 16/6/15.
//  Copyright © 2016年 seemygo. All rights reserved.
//

#import "UIBarButtonItem+Item.h"

@implementation UIBarButtonItem (Item)
+ (instancetype)itemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(nullable id)target action:(SEL)action
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImage:highImage forState:UIControlStateHighlighted];
    [leftButton sizeToFit];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *containView = [[UIView alloc] initWithFrame:leftButton.bounds];
    [containView addSubview:leftButton];
    // bug:导航条上按钮点击范围扩大
    // 普通的话,设置一个按钮宽度,那么点击范围就是按钮范围,但是把按钮保证成UIBarButtonItem,点击范围扩大
    return [[UIBarButtonItem alloc] initWithCustomView:containView];
}

+ (instancetype)itemWithImage:(UIImage *)image selImage:(UIImage *)selImage target:(id)target action:(SEL)action
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:image forState:UIControlStateNormal];
    [leftButton setImage:selImage forState:UIControlStateSelected];
    [leftButton sizeToFit];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIView *containView = [[UIView alloc] initWithFrame:leftButton.bounds];
    [containView addSubview:leftButton];
    // bug:导航条上按钮点击范围扩大
    // 普通的话,设置一个按钮宽度,那么点击范围就是按钮范围,但是把按钮保证成UIBarButtonItem,点击范围扩大
    return [[UIBarButtonItem alloc] initWithCustomView:containView];
}
@end
